package com.zx.mobile.service.proxy;

import com.zx.map.service.model.BaseAcctInfoModel;
import com.zx.map.service.model.CreditRepayLogModel;
import com.zx.map.service.model.CustAcctLogModel;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.mobile.domain.RepayApplyDomain;
import com.zx.platform.utils.DateUtils;
import com.zx.platform.utils.PmpUtils;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.Date;

public class CreditRepayCommonProxy {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(CreditRepayCommonProxy.class);

    /**
     * 模型转换 减少账户余额
     *
     * @param temp
     * @param transAmt
     * @return
     */
    public static BaseAcctInfoModel transformSubBaseAcct(BaseAcctInfoModel temp,
                                                         String transAmt) {
        BigDecimal avlAmt = new BigDecimal(temp.getAvlBal().toString());
        BigDecimal acctAmt = new BigDecimal(temp.getAcctBal().toString());
        temp.setAvlBal(avlAmt.subtract(new BigDecimal(transAmt)).doubleValue());
        temp.setAcctBal(acctAmt.subtract(new BigDecimal(transAmt)).doubleValue());
        return temp;
    }

    /**
     * 模型转换 增加账户余额
     *
     * @param temp
     * @param transAmt
     * @return
     */
    public static BaseAcctInfoModel transformAddBaseAcct(BaseAcctInfoModel temp,
                                                         String transAmt) {
        BigDecimal avlAmt = new BigDecimal(temp.getAvlBal().toString());
        BigDecimal acctAmt = new BigDecimal(temp.getAcctBal().toString());
        temp.setAvlBal(avlAmt.add(new BigDecimal(transAmt)).doubleValue());
        temp.setAcctBal(acctAmt.add(new BigDecimal(transAmt)).doubleValue());
        return temp;
    }

    /**
     * 记录还款历史
     *
     * @param user
     * @param repayApplyDomain
     * @return
     */
    public static CreditRepayLogModel transformCreditRepayLog(
            MobileUserDomain user, RepayApplyDomain repayApplyDomain) {
        String repayOrd = PmpUtils.getCreditRepayOrderNum();
        CreditRepayLogModel param = new CreditRepayLogModel();
        param.setAgentId(user.getMerchantInfoDetailModel().getAgentId());
        param.setAgentSysAccount(user.getMerchantInfoDetailModel()
                .getAgentSysAccount());
        param.setCreateDate(new Date());
        param.setRepayAmt(Double.valueOf(repayApplyDomain.getRepayAmt()));
        param.setTradeAmt(Double.valueOf(repayApplyDomain.getTradeAmt()));
        param.setRepayId(repayOrd);
        param.setRepayStatus("N");
        param.setRepayType("1");
        param.setMerchantId(user.getMerchantInfoDetailModel().getMerchantId());
        param.setMerSysAccount(user.getMerchantInfoDetailModel()
                .getSysAccount());
        param.setRepayDay(DateUtils.dateToString(new Date(), "yyyyMMdd")); // 增加还款日期
        return param;
    }

    /**
     * 类型转换  账户流水
     *
     * @param baseModel
     * @param creditRepayLogModel
     * @return
     */
    public static CustAcctLogModel transformCustAcct(
            BaseAcctInfoModel baseModel, CreditRepayLogModel creditRepayLogModel) {
        CustAcctLogModel temp = new CustAcctLogModel();
        temp.setAcctBal(baseModel.getAvlBal());
        temp.setAcctType("B");
        temp.setCreateDate(new Date());
        temp.setCustId(creditRepayLogModel.getMerchantId());
        temp.setCustSysaccount(creditRepayLogModel.getMerSysAccount());
        temp.setOrdId(creditRepayLogModel.getRepayId());
        temp.setRemark("");
        temp.setOriginalAmt(creditRepayLogModel.getRepayAmt());//增加账户变化原始交易金额  记录还款金额
        temp.setTransAmt(creditRepayLogModel.getTradeAmt());
        temp.setTransType(SUConstants.TRANSTYPE_CREDITENCASH);
        return temp;
    }

    /**
     * 类型转换  账户流水 冲正账务
     *
     * @param baseModel
     * @param creditRepayLogModel
     * @return
     */
    public static CustAcctLogModel transformReverseCustAcct(
            BaseAcctInfoModel baseModel, CreditRepayLogModel creditRepayLogModel) {
        CustAcctLogModel temp = new CustAcctLogModel();
        temp.setAcctBal(baseModel.getAvlBal());
        temp.setAcctType("B");
        temp.setCreateDate(new Date());
        temp.setCustId(creditRepayLogModel.getMerchantId());
        temp.setCustSysaccount(creditRepayLogModel.getMerSysAccount());
        temp.setOrdId(creditRepayLogModel.getRepayId());
        temp.setRemark("");
        temp.setOriginalAmt(0 - creditRepayLogModel.getRepayAmt());//增加账户变化原始交易金额  记录还款金额
        temp.setTransAmt(0 - creditRepayLogModel.getTradeAmt());
        temp.setTransType(SUConstants.TRANSTYPE_CREDITENCASH);
        return temp;
    }
}
